package edu.bmstu.rslab;

import javax.swing.*;
import java.awt.*;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.util.ArrayList;

/**
 * Created by Chikh (using IntelliJ IDEA).
 * Date: 14.04.2009
 * Time: 18:32:07
 */
public class CoordinateSystemPanel extends JPanel {

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        g2.scale((double) this.getWidth()/1.2, (double) this.getHeight()/1.2);
        g2.setStroke(new BasicStroke(0.001f));
        g2.translate(0.1, 0.1);
        g2.draw(new Line2D.Double(0, 0, 1.025, 0));
        g2.draw(new Line2D.Double(1.025, 0, 1.015, -0.01));
        g2.draw(new Line2D.Double(1.025, 0, 1.015, 0.01));
        g2.draw(new Line2D.Double(0, 0, 0, 1.025));
        g2.draw(new Line2D.Double(0, 1.025, -0.01, 1.015));
        g2.draw(new Line2D.Double(0, 1.025, 0.01, 1.015));

        for (int i = 1; i <= 10; i++) {
            g2.draw(new Line2D.Double(i*0.1, 0, i*0.1, 1));
            g2.draw(new Line2D.Double(0, i*0.1, 1, i*0.1));
        }

        g2.setColor(Color.BLUE);
        for (Point2D point : pathPoints) {
            Ellipse2D ellipse = new Ellipse2D.Double();
            ellipse.setFrameFromCenter(point.getX(), point.getY(), point.getX()+0.003, point.getY()+0.003);
            g2.fill(ellipse);
        }

        g2.setColor(Color.RED);
        for (Point2D point : robotPositionPoints) {
            Ellipse2D ellipse = new Ellipse2D.Double();
            ellipse.setFrameFromCenter(point.getX(), point.getY(), point.getX()+0.003, point.getY()+0.003);
            g2.fill(ellipse);
            if (robotPositionPoints.indexOf(point) != 0) {
                g2.draw(new Line2D.Double(robotPositionPoints.get(robotPositionPoints.indexOf(point)-1),point));
            }
        }
    }

    public void addPathPoint(Point2D point) {
        pathPoints.add(point);
        repaint();
    }

    public void addRobotPositionPoint(Point2D point) {
        robotPositionPoints.add(point);
        repaint();
    }

    public Point2D getScaledPoint(Point2D point) {
        return new Point2D.Double(point.getX()/((double) this.getWidth()/1.2)-0.1,
                point.getY()/((double) this.getHeight()/1.2)-0.1);
    }

    private ArrayList<Point2D> pathPoints = new ArrayList<Point2D>();
    private ArrayList<Point2D> robotPositionPoints = new ArrayList<Point2D>();
}
